package com.act;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.bean.BaseBean;
import com.common.Constant;
import com.control.UserControl;
import com.xike.kyt.R;
import com.mUI.MyProgressDialog;
import com.xike.util.Manager;
import com.xike.util.MyUtil;
import com.xike.util.StringUtil;

public class Question_AskAct extends BaseAct implements OnClickListener {
	private static final int REQUEST_CODE_TO_TOPLICLIST = 1;
	private static final int REQUEST_CODE_TO_ADDDESC = 2;

	TextView tv_back, tv_publish;
	TextView tv_addTopic, tv_addDesc;
	EditText edt_title;
	String strchoseToplicIds;
	String strchoseRemindUserIds;

	String strDesc;
	int uploadNum = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_ask);
		initViews();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_CODE_TO_TOPLICLIST && resultCode > 0) {
			Bundle bundle = data.getExtras();
			if (bundle != null && bundle.containsKey("toplicIdStrs")) {
				strchoseToplicIds = bundle.getString("toplicIdStrs");
				if (StringUtil.isNotNull(strchoseToplicIds)) {
					tv_addTopic.setSelected(true);
				}
			}
		} else if (requestCode == REQUEST_CODE_TO_ADDDESC && resultCode > 0) {
			Bundle bundle = data.getExtras();
			if (bundle != null && bundle.containsKey("remindUserIds")) {
				strchoseRemindUserIds = bundle.getString("remindUserIds");
			}
			if (bundle != null && bundle.containsKey("desc")) {
				strDesc = bundle.getString("desc");
				if (StringUtil.isNotNull(strDesc)) {
					tv_addDesc.setSelected(true);
				}
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void initViews() {
		tv_back = (TextView) findViewById(R.id.headTitle_tv_back);
		tv_publish = (TextView) findViewById(R.id.ask_tv_publish);

		edt_title = (EditText) findViewById(R.id.ask_edt_title);

		tv_back.setOnClickListener(this);
		tv_publish.setOnClickListener(this);

		tv_addTopic = (TextView) findViewById(R.id.ask_tv_addTopic);
		tv_addTopic.setOnClickListener(this);
		tv_addDesc = (TextView) findViewById(R.id.ask_tv_addDesc);
		tv_addDesc.setOnClickListener(this);

	}

	@Override
	protected void updateViews() {
	}

	private void serviceAsk() {
		MyProgressDialog.show(this, true);
		final Handler handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case MSG_SERVICEDATA_ERROR:
					MyProgressDialog.cancle();
					mToast.showToast(Question_AskAct.this, getString(R.string.net_error));
					break;
				case MSG_SERVICEDATA_OK:
					MyProgressDialog.cancle();
					BaseBean result = (BaseBean) msg.obj;
					if (result.isResult()) {
						mToast.showToast(Question_AskAct.this, "发布成功");
						// UserControl.setUserInfoChanged(true);
						setResult(1);
						finish();
					} else {
						mToast.showToast(Question_AskAct.this, result.getError_description());
					}
					break;
				default:
					break;
				}
				super.handleMessage(msg);
			}

		};
		Manager.singleThread.execute(new Runnable() {

			@Override
			public void run() {
				ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
				params.add(new BasicNameValuePair("access_token", UserControl.getAccessToken()));
				params.add(new BasicNameValuePair("app_key", Constant.APPKEY));
				params.add(new BasicNameValuePair("client_info", Manager.getClientInfo()));
				String strSignature = StringUtil.MD5(Constant.APPSERRET + UserControl.getAccessTokenSecret());
				params.add(new BasicNameValuePair("signature", strSignature));

				params.add(new BasicNameValuePair("title", MyUtil.getEditTextContent(edt_title)));
				params.add(new BasicNameValuePair("description", strDesc));
				if (StringUtil.isNotNull(strchoseToplicIds)) {
					params.add(new BasicNameValuePair("topic", strchoseToplicIds));
				}
				if (StringUtil.isNotNull(strchoseRemindUserIds)) {
					params.add(new BasicNameValuePair("remindUser", strchoseRemindUserIds));
				}

				params.add(new BasicNameValuePair("sign", MyUtil.getSignRequestParams(params)));

				BaseBean result = client.postFilesContent(Constant.SERVER_URL_QUESTION_ASK, null, params);
				Message msg = new Message();
				msg.what = MSG_SERVICEDATA_ERROR;
				if (result != null) {
					msg.what = MSG_SERVICEDATA_OK;
					msg.obj = result;
				}
				handler.sendMessage(msg);

			}
		});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ask_tv_addTopic:
			Intent it_toplicList = new Intent(this, TopicListAct.class);
			startActivityForResult(it_toplicList, REQUEST_CODE_TO_TOPLICLIST);
			break;

		case R.id.ask_tv_addDesc:
			Intent it_addDesc = new Intent(this, Question_AskAddDescAct.class);
			startActivityForResult(it_addDesc, REQUEST_CODE_TO_ADDDESC);
			break;
		case R.id.headTitle_tv_back:
			finish();
			break;

		case R.id.ask_tv_publish:
			if (checkContent()) {
				serviceAsk();
			}
			break;

		default:
			break;
		}
	}

	private boolean checkContent() {
		String strTitle = MyUtil.getEditTextContent(edt_title);
		if (!StringUtil.isNotNull(strTitle)) {
			mToast.showToast(this, "主题不能为空");
			return false;
		}
		if (!tv_addTopic.isSelected()) {
			mToast.showToast(this, "至少添加一个话题");
			return false;
		}
		
		return true;
	}

	protected void onRestart() {
		super.onRestart();
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
		}
		return super.dispatchKeyEvent(event);
	}

}
